package zhoug.demo.zui.ui;

import android.app.Dialog;
import android.content.Context;
import android.graphics.Point;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.Toast;

import java.util.Arrays;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import zhoug.common.anim.ZAnimations;
import zhoug.common.utils.AppUtils;
import zhoug.demo.zui.R;
import zhoug.zui.ZUITopView;
import zhoug.zui.dialog.ZUIButtonDialog;
import zhoug.zui.dialog.ZUIDialog;
import zhoug.zui.dialog.list.ZUIListDialog;

/**
 * 测试自定义dialog
 *
 * @Author 35574
 * @Date 2022/4/20
 * @Description
 */
public class TestDialogActivity extends AppCompatActivity {
    private Point mScreenSize;
    private ZUITopView mTopView;
    private Button mBtnZUIListDialog;
    private Button mBtnDialog;
    private Button mBtnZUIDialog;
    private Button mBtnZUIButtonDialog;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_test_dialog);
        mScreenSize = AppUtils.getScreenSize();

        findViews();
        bindListener();
    }

    private void findViews() {
        mTopView = findViewById(R.id.topView);
        mBtnZUIListDialog = findViewById(R.id.btn_ZUIListDialog);
        mBtnDialog = findViewById(R.id.btn_Dialog);
        mBtnZUIDialog = findViewById(R.id.btn_ZUIDialog);
        mBtnZUIButtonDialog = findViewById(R.id.btn_ZUIButtonDialog);
    }

    private void bindListener() {
        mBtnZUIListDialog.setOnClickListener(v -> {
            ZUIListDialog<String> stringListDialog = new ZUIListDialog<String>(this) {
                @Override
                public String getText(String itemData) {
                    return itemData;
                }
            };
            stringListDialog.setFullWidth(true);
            stringListDialog.setCorners(60);
            stringListDialog.setAttributes(WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.WRAP_CONTENT, Gravity.BOTTOM);
            stringListDialog.setWindowAnimations(ZAnimations.ANIM_DOWN);
            stringListDialog.setCanceledOnTouchOutside(true);
            stringListDialog.setData(Arrays.asList("北京", "上海", "重庆", "四川", "青海"));
            stringListDialog.setOnItemClickListener(position -> {
                String itemData = stringListDialog.getItemData(position);
                toast(itemData);
            });
            stringListDialog.show();
        });
        mBtnDialog.setOnClickListener(v -> {
            Dialog dialog = new Dialog(this);
            View contentView = LayoutInflater.from(this).inflate(R.layout.dialog_content_view, null);
            dialog.setContentView(contentView);
            dialog.show();
            Window window = dialog.getWindow();
            if(window!=null){
                WindowManager.LayoutParams attributes = window.getAttributes();
                attributes.width = WindowManager.LayoutParams.MATCH_PARENT;
//                attributes.width = mScreenSize.x*3/4;
                attributes.gravity=Gravity.BOTTOM;
                window.setAttributes(attributes);
            }
        });
        mBtnZUIDialog.setOnClickListener(v->{
            ZUIDialogImp dialog=new ZUIDialogImp(this);

            dialog.setAttributes(WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.WRAP_CONTENT, Gravity.BOTTOM);
            dialog.show();
        });

        mBtnZUIButtonDialog.setOnClickListener(v->{
            ZUIButtonDialog dialog=new ZUIButtonDialog(this);
            dialog.setCancelable(true);
            dialog.setCorners(60);
            dialog.setTitle("标题");
            dialog.setMessage("几乎是大街上的圣诞节开始对你说的减肥");
            dialog.setNegativeListener("取消",v1->{
                toast("取消");
                dialog.dismiss();
            });
            dialog.setPositiveListener("确定",v1->{
                toast("确定");
                dialog.dismiss();
            });

            dialog.show();

        });
    }

    private void toast(String msg) {
        Toast.makeText(this, null != msg ? msg : "", Toast.LENGTH_SHORT).show();
    }

    private static final class ZUIDialogImp extends ZUIDialog {

        public ZUIDialogImp(@NonNull Context context) {
            super(context);
        }

        @Override
        protected int getLayoutResId() {
            return R.layout.dialog_content_view;
        }

        @Override
        protected void onViewCreated(Bundle savedInstanceState) {

        }
    }
}
